package com.hkw.servlet;

import com.hkw.model.Student;
import com.hkw.service.IStudentService;
import com.hkw.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 处理添加学生的Servlet
 */
@WebServlet(name = "AddStudentServlet",urlPatterns = "/addStudent")
public class AddStudentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String stname = request.getParameter("stname");
        String sex = request.getParameter("sex");
        String birthday = request.getParameter("birthday");
        String email = request.getParameter("email");
        String classId = request.getParameter("classId");
        String remark = request.getParameter("remark");
        //将获取到的值，生成一个student
        Student student = new Student(stname,sex,birthday,email,remark,Integer.parseInt(classId));
        System.out.println(student);
        IStudentService studentService = new StudentServiceImpl();
        //调用接口服务，将生成的student传入
        int update = studentService.add(student);
        if(update==1){
            System.out.println(" 添加成功");
        }else{
            System.out.println("添加失败");
        }
        //重定向
        response.sendRedirect("studentlist");
        System.out.println(student);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
